﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

public partial class Pages_Profile : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // Turns off the browser caching
        Response.Cache.SetCacheability(HttpCacheability.NoCache);

        if (!IsPostBack && Membership.GetUser() != null)
        {
            ((UserControls_EditUserProfile)EditUserProfile1).ShowUsersProfile(Membership.GetUser().UserName);
            ShowProfilePicture();
        }
        
    }

    /// <summary>
    /// Shows the profile picture for the current user.
    /// </summary>
    private void ShowProfilePicture()
    {
        try
        {

            if (!String.IsNullOrEmpty(Profile.PicturePath))
            {
                if (File.Exists(Server.MapPath(Profile.PicturePath)))
                {
                    imgProfilePicture.ImageUrl = Profile.PicturePath;
                }
            }
        }
        catch (Exception)
        { }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        ProfileManagement.SaveProfile(((UserControls_EditUserProfile)EditUserProfile1).GetProfileEntity());
    }
    protected void btnUploadPicture_Click(object sender, EventArgs e)
    {
        System.Drawing.Image theImage = null;
        try
        {
            if (MyFileUpload.HasFile && Membership.GetUser() != null)
            {
                // Checking size
                if (MyFileUpload.PostedFile.ContentLength <= Convert.ToInt32(ConfigurationSettings.AppSettings["MaximumPictureSize"]))
                {

                    // Checking dimensions
                    theImage = System.Drawing.Image.FromStream(MyFileUpload.FileContent);
                    if (theImage.Width <= Convert.ToInt32(ConfigurationSettings.AppSettings["MaximumPictureWidth"])
                        &&
                        theImage.Height <= Convert.ToInt32(ConfigurationSettings.AppSettings["MaximumPictureHeight"]))
                    {
                        //Getting file path
                        string filePathRaw = Path.Combine(ConfigurationSettings.AppSettings["ProfilePictureFolder"],
                              String.Format("{0}_{1}",Membership.GetUser().UserName,Path.GetRandomFileName()));

                        MyFileUpload.SaveAs(Server.MapPath(filePathRaw));
                        //Stores without mapping
                        Profile.PicturePath = filePathRaw;
                        Profile.Save();

                        ShowProfilePicture();
                    }
                    else
                    {
                        //TODO  WRONG dimensions
                    }
                }
                else
                {
                    //TODO Too big
                }
            }
        }
        catch (Exception)
        {
            //TODO Show message
        }
        finally
        {
            theImage.Dispose();
            MyFileUpload.FileContent.Dispose();
        }

    }
}
